/*
 * Copyright (C) 2004 Derek James and Philip Tucker
 * 
 * This file is part of ANJI (Another NEAT Java Implementation).
 * 
 * ANJI is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * Created on Jul 23, 2005 by Philip Tucker
 */
package com.anji.tournament;

import com.anji.tournament.SimpleTournament.PlayerPair;

/**
 * Wins, losses, ties, and score. Keeps player1Stats and player2Stats in sync.
 * @author Philip Tucker
 */
public class GameResults {

  /**
   * results for contestant, or player 1
   */
  private PlayerStats player1Stats = new PlayerStats();

  /**
   * results for opponent, or player 2
   */
  private PlayerStats player2Stats = new PlayerStats();

  /**
   * default ctor
   */
  public GameResults() {
    super();
  }

  /**
   * @param count # wins to add to player 1, and losses for player 2
   */
  public void incrementPlayer1Wins(int count) {
    player1Stats.incrementWins(count);
    player2Stats.incrementLosses(count);
  }

  /**
   * @param count # losses to add to player 1, and wins for player 2
   */
  public void incrementPlayer1Losses(int count) {
    player1Stats.incrementLosses(count);
    player2Stats.incrementWins(count);
  }

  /**
   * @param count # ties to add to player 1 and player 2
   */
  public void incrementTies(int count) {
    player1Stats.incrementTies(count);
    player2Stats.incrementTies(count);
  }

  /**
   * @param player1Points new points for player 1
   * @param player2Points new points for player 2
   */
  public void incrementRawScore(int player1Points, int player2Points) {
    player1Stats.incrementRawScore(player1Points);
    player2Stats.incrementRawScore(player2Points);
  }

  /**
   * increment player stats based on new results
   * @param newResults
   */
  public void increment(GameResults newResults) {
    player1Stats.increment(newResults.player1Stats);
    player2Stats.increment(newResults.player2Stats);
  }

  /**
   * increment player stats based on new results; swap player one and player two
   * stats
   * @param newResults
   */
  public void incrementSwapped(GameResults newResults) {
    player1Stats.increment(newResults.player2Stats);
    player2Stats.increment(newResults.player1Stats);
  }

  /**
   * @return player 1 stats
   */
  public PlayerStats getPlayer1Stats() {
    return player1Stats;
  }

  /**
   * @return player 2 stats
   */
  public PlayerStats getPlayer2Stats() {
    return player2Stats;
  }

  /**
   * @param p1Results Player 1 results.
   * @param p2Results Player 2 results.
   */
  public void applyTo(PlayerResults p1Results, PlayerResults p2Results) {
    p1Results.getStats().increment(player1Stats);
    p2Results.getStats().increment(player2Stats);
  }

  /**
   * @param pair Player pair.
   */
  public void applyTo(PlayerPair pair) {
    applyTo(pair.contestant, pair.opponent);
  }

  /**
   * @param tieBreaker Tie-breaker.
   * @return Win is determined first by wins, second by score, and third by
   * tieBreaker.
   */
  public boolean isPlayer1Win(boolean tieBreaker) {
    assert player1Stats.getWins() == player2Stats.getLosses() : "mismatched player 1 wins and player 2 losses";
    assert player2Stats.getWins() == player1Stats.getLosses() : "mismatched player 2 wins and player 1 losses";
    assert player1Stats.getTies() == player2Stats.getTies() : "mismatched player 1 and player 2 ties";
    if (player1Stats.getWins() > player2Stats.getWins()) {
      return true;
    }
    if (player1Stats.getWins() < player2Stats.getWins()) {
      return false;
    }
    if (player1Stats.getRawScore() > player2Stats.getRawScore()) {
      return true;
    }
    if (player1Stats.getRawScore() < player2Stats.getRawScore()) {
      return false;
    }
    return tieBreaker;
  }

  /**
   * @see java.lang.Object#toString()
   */
  public String toString() {
    return player1Stats.toString() + " vs " + player2Stats.getRawScore();
  }
}
